package com.project.springboot.page;

import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

/**
 * description: Http请求Holder
 * @author 朱正磊
 * @date 2023-08-09 23:10:30
 */
@Slf4j
public class RequestHolder {

    /**
     * description: 获取当前请求线程的Http请求对象
     * @author 朱正磊
     * @date 2023-07-11 18:03:04
     * @return 返回Http请求对象
     */
    public static HttpServletRequest getRequest() {
        RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes servletRequestAttributes = (ServletRequestAttributes) requestAttributes;
        assert servletRequestAttributes != null;
        return servletRequestAttributes.getRequest();
    }

    /**
     * description: 根据请求参数名获取请求参数值
     * @author 朱正磊
     * @date 2023-07-11 18:37:28
     * @param name 请求参数名
     * @return 返回请求参数值
     */
    public static String getParameter(String name) {
        String parameter = getRequest().getParameter(name);
        log.info("根据请求参数名【{}】获取到的请求参数值 = {}", name, parameter);
        return parameter;
    }

    /**
     * description: 根据请求头名称获取请求头值
     * @author 朱正磊
     * @date 2023-07-11 18:42:58
     * @param name 请求头名称
     * @return 返回请求头值
     */
    public static String getHeader(String name) {
        String header = getRequest().getHeader(name);
        log.info("根据请求头名称【{}】获取到的请求头值 = {}", name, header);
        return header;
    }

}
